home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CustomSolutionWizard_Files / ProjectShells / NSNetwork.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  38.8 KB  |  837 lines

  1. //===========================================================================
  2. // Custom Solution Wizard - NSNetwork Class Implementation
  3. //
  4. //---------------------------------------------------------------------------
  5. // This file is provided to demonstrate how classes can be constructed to
  6. // encapsulate the functions in a Custom Solution Wizard generated DLL.
  7. // The classes in this file can be used without modication, but are not
  8. // required for interaction with generated DLLs.
  9. //===========================================================================
  10.  
  11. //===========================================================================
  12. // Includes
  13. //===========================================================================
  14. #include "stdafx.h"
  15. #include "NSNetwork.h"
  16.  
  17. //===========================================================================
  18. //===========================================================================
  19. // CLASS: NSNetwork
  20. //===========================================================================
  21. //===========================================================================
  22.  
  23. //---------------------------------------------------------------------------
  24. // CLASS: NSNetwork            FUNCTION: <constructor> (LPCSTR)
  25. //---------------------------------------------------------------------------
  26. NSNetwork::NSNetwork(LPCSTR dllPathName)
  27. {
  28.     //---------------------------------------------------------------------------
  29.     // Load the neural network DLL.
  30.     //---------------------------------------------------------------------------
  31.     m_hDLL = LoadLibrary(dllPathName);
  32.     m_pNetworkInstance = 0;
  33.  
  34.     //---------------------------------------------------------------------------
  35.     // If the DLL load was successful, get the addresses of the DLL functions.
  36.     //---------------------------------------------------------------------------
  37.     if (m_hDLL)
  38.     {
  39.         //---------------------------------------------------------------------------
  40.         // Load functions common to both RECALL and LEARNING DLLs.
  41.         //---------------------------------------------------------------------------
  42.         m_NSCreateNetwork = (NSCreateNetwork)GetProcAddress(m_hDLL, "createNetwork");
  43.         m_NSDestroyNetwork = (NSDestroyNetwork)GetProcAddress(m_hDLL, "destroyNetwork");
  44.         m_NSGetInputOutputInfo = (NSGetInputOutputInfo)GetProcAddress(m_hDLL, "getInputOutputInfo");
  45.         m_NSGetResponse = (NSGetResponse)GetProcAddress(m_hDLL, "getResponse");
  46.         m_NSGetSensitivity = (NSGetSensitivity)GetProcAddress(m_hDLL, "getSensitivity");
  47.         m_NSLoadWeights = (NSLoadWeights)GetProcAddress(m_hDLL, "loadWeights");
  48.         m_NSSaveWeights = (NSSaveWeights)GetProcAddress(m_hDLL, "saveWeights");
  49.         m_NSRandomizeWeights = (NSRandomizeWeights)GetProcAddress(m_hDLL, "randomizeWeights");
  50.         m_NSResetNetwork = (NSResetNetwork)GetProcAddress(m_hDLL, "resetNetwork");
  51.  
  52.         //---------------------------------------------------------------------------
  53.         // Verify functions common to both RECALL and LEARNING DLLs loaded correctly.
  54.         //---------------------------------------------------------------------------
  55.         if (!m_NSCreateNetwork ||
  56.             !m_NSDestroyNetwork ||
  57.             !m_NSGetInputOutputInfo ||
  58.             !m_NSGetResponse ||
  59.             !m_NSGetSensitivity ||
  60.             !m_NSLoadWeights ||
  61.             !m_NSSaveWeights ||
  62.             !m_NSRandomizeWeights ||
  63.             !m_NSResetNetwork)
  64.         {
  65.             FreeLibrary(m_hDLL);
  66.             m_hDLL = 0;
  67.         }
  68.     }
  69. }
  70.  
  71. //---------------------------------------------------------------------------
  72. // CLASS: NSNetwork            FUNCTION: <destructor>
  73. //---------------------------------------------------------------------------
  74. NSNetwork::~NSNetwork()
  75. {
  76.     //---------------------------------------------------------------------------
  77.     // If the network instance is still set, release it.
  78.     //---------------------------------------------------------------------------
  79.     if (IsInitialized())
  80.     {
  81.         m_NSDestroyNetwork(m_pNetworkInstance);
  82.         m_pNetworkInstance = 0;
  83.     }
  84.  
  85.     //---------------------------------------------------------------------------
  86.     // Release the neural network DLL if it is loaded.
  87.     //---------------------------------------------------------------------------
  88.     if (IsLoaded())
  89.     {
  90.         FreeLibrary(m_hDLL);
  91.         m_hDLL = 0;
  92.     }
  93. }
  94.  
  95. //---------------------------------------------------------------------------
  96. // CLASS: NSNetwork            FUNCTION: GetInputs()
  97. //---------------------------------------------------------------------------
  98. // Return the number of inputs for the generated DLL or -1 on failure.
  99. //---------------------------------------------------------------------------
  100. int NSNetwork::GetInputs()
  101. {
  102.     int numInputs;
  103.     int numOutputs;
  104.     if (!IsInitialized())
  105.         return -1;
  106.     if (m_NSGetInputOutputInfo(m_pNetworkInstance,numInputs,numOutputs))
  107.         return -1;
  108.     return numInputs;
  109. }
  110.  
  111. //---------------------------------------------------------------------------
  112. // CLASS: NSNetwork            FUNCTION: GetOutputs()
  113. //---------------------------------------------------------------------------
  114. // Return the number of outputs for the generated DLL or -1 on failure.
  115. //---------------------------------------------------------------------------
  116. int NSNetwork::GetOutputs()
  117. {
  118.     int numInputs;
  119.     int numOutputs;
  120.     if (!IsInitialized())
  121.         return -1;
  122.     if (m_NSGetInputOutputInfo(m_pNetworkInstance,numInputs,numOutputs))
  123.         return -1;
  124.     return numOutputs;
  125. }
  126.  
  127. //---------------------------------------------------------------------------
  128. // CLASS: NSNetwork            FUNCTION: GetInputOutputInfo(int,int)
  129. //---------------------------------------------------------------------------
  130. // Obtain the number of inputs and outputs for the generated DLL.
  131. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  132. //---------------------------------------------------------------------------
  133. int NSNetwork::GetInputOutputInfo(int& numInputs, int& numOutputs)
  134. {
  135.     if (!IsInitialized())
  136.         return -1;
  137.     return m_NSGetInputOutputInfo(m_pNetworkInstance,numInputs,numOutputs);
  138. }
  139.  
  140. //---------------------------------------------------------------------------
  141. // CLASS: NSNetwork            FUNCTION: GetResponse(int,float*,float*)
  142. //---------------------------------------------------------------------------
  143. // Obtain the neural network response to a set of input data.
  144. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  145. //---------------------------------------------------------------------------
  146. int NSNetwork::GetResponse(int exemplars, float* inputData, float* outputData)
  147. {
  148.     if (!IsInitialized())
  149.         return -1;
  150.     return m_NSGetResponse(m_pNetworkInstance,exemplars,inputData,outputData);
  151. }
  152.  
  153. //---------------------------------------------------------------------------
  154. // CLASS: NSNetwork            FUNCTION: GetSensitivity(int,float*,float*,float)
  155. //---------------------------------------------------------------------------
  156. // Obtain the neural network sensitivity to a set of input data.
  157. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  158. //---------------------------------------------------------------------------
  159. int NSNetwork::GetSensitivity(int exemplars, float* inputData, float* sensitivityData, float dither)
  160. {
  161.     if (!IsInitialized())
  162.         return -1;
  163.     return m_NSGetSensitivity(m_pNetworkInstance,exemplars,inputData,sensitivityData,dither);
  164. }
  165.  
  166. //---------------------------------------------------------------------------
  167. // CLASS: NSNetwork            FUNCTION: LoadWeights(LPCSTR)
  168. //---------------------------------------------------------------------------
  169. // Load a weights file.
  170. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  171. //---------------------------------------------------------------------------
  172. int NSNetwork::LoadWeights(LPCSTR pathName)
  173. {
  174.     if (!IsInitialized())
  175.         return -1;
  176.     return m_NSLoadWeights(m_pNetworkInstance,pathName);
  177. }
  178.  
  179. //---------------------------------------------------------------------------
  180. // CLASS: NSNetwork            FUNCTION: RandomizeWeights()
  181. //---------------------------------------------------------------------------
  182. // Randomize the weights in the network.
  183. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  184. //---------------------------------------------------------------------------
  185. int NSNetwork::RandomizeWeights()
  186. {
  187.     if (!IsInitialized())
  188.         return -1;
  189.     return m_NSRandomizeWeights(m_pNetworkInstance);
  190. }
  191.  
  192. //---------------------------------------------------------------------------
  193. // CLASS: NSNetwork            FUNCTION: ResetNetwork()
  194. //---------------------------------------------------------------------------
  195. // Reset the network.
  196. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  197. //---------------------------------------------------------------------------
  198. int NSNetwork::ResetNetwork()
  199. {
  200.     if (!IsInitialized())
  201.         return -1;
  202.     return m_NSResetNetwork(m_pNetworkInstance);
  203. }
  204.  
  205. //---------------------------------------------------------------------------
  206. // CLASS: NSNetwork            FUNCTION: SaveWeights(LPCSTR)
  207. //---------------------------------------------------------------------------
  208. // Save a weights file.
  209. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  210. //---------------------------------------------------------------------------
  211. int NSNetwork::SaveWeights(LPCSTR pathName)
  212. {
  213.     if (!IsInitialized())
  214.         return -1;
  215.     return m_NSSaveWeights(m_pNetworkInstance,pathName);
  216. }
  217.  
  218. //===========================================================================
  219. //===========================================================================
  220. // CLASS: NSLearningNetwork
  221. //===========================================================================
  222. //===========================================================================
  223.  
  224. //---------------------------------------------------------------------------
  225. // CLASS: NSLearningNetwork        FUNCTION: <constructor> (LPCSTR)
  226. //---------------------------------------------------------------------------
  227. NSLearningNetwork::NSLearningNetwork(LPCSTR dllPathName):
  228.     NSNetwork(dllPathName)
  229. {
  230.     //---------------------------------------------------------------------------
  231.     // If the DLL load was successful, get the addresses of the learning DLL functions.
  232.     //---------------------------------------------------------------------------
  233.     if (IsLoaded())
  234.     {
  235.         //---------------------------------------------------------------------------
  236.         // Load functions unique to learning DLLs.
  237.         //---------------------------------------------------------------------------
  238.         m_NSTrain = (NSTrain)GetProcAddress(m_hDLL, "train");
  239.         m_NSGetBestCost = (NSGetBestCost)GetProcAddress(m_hDLL, "getBestCost");
  240.         m_NSSetBestCost = (NSSetBestCost)GetProcAddress(m_hDLL, "setBestCost");
  241.         m_NSGetBestWeightsPathName = (NSGetBestWeightsPathName)GetProcAddress(m_hDLL, "getBestWeightsPathName");
  242.         m_NSSetBestWeightsPathName = (NSSetBestWeightsPathName)GetProcAddress(m_hDLL, "setBestWeightsPathName");
  243.         m_NSGetSaveBestWeightsEnabled = (NSGetSaveBestWeightsEnabled)GetProcAddress(m_hDLL, "getSaveBestWeightsEnabled");
  244.         m_NSSetSaveBestWeightsEnabled = (NSSetSaveBestWeightsEnabled)GetProcAddress(m_hDLL, "setSaveBestWeightsEnabled");
  245.         m_NSGetSaveBestWeightsForTraining = (NSGetSaveBestWeightsForTraining)GetProcAddress(m_hDLL, "getSaveBestWeightsForTraining");
  246.         m_NSSetSaveBestWeightsForTraining = (NSSetSaveBestWeightsForTraining)GetProcAddress(m_hDLL, "setSaveBestWeightsForTraining");
  247.         m_NSGetCrossValidationEnabled = (NSGetCrossValidationEnabled)GetProcAddress(m_hDLL, "getCrossValidationEnabled");
  248.         m_NSSetCrossValidationEnabled = (NSSetCrossValidationEnabled)GetProcAddress(m_hDLL, "setCrossValidationEnabled");
  249.         m_NSGetAutoComputeInputNormCoeff = (NSGetAutoComputeInputNormCoeff)GetProcAddress(m_hDLL, "getAutoComputeInputNormCoeff");
  250.         m_NSSetAutoComputeInputNormCoeff = (NSSetAutoComputeInputNormCoeff)GetProcAddress(m_hDLL, "setAutoComputeInputNormCoeff");
  251.         m_NSGetAutoComputeOutputNormCoeff = (NSGetAutoComputeOutputNormCoeff)GetProcAddress(m_hDLL, "getAutoComputeOutputNormCoeff");
  252.         m_NSSetAutoComputeOutputNormCoeff = (NSSetAutoComputeOutputNormCoeff)GetProcAddress(m_hDLL, "setAutoComputeOutputNormCoeff");
  253.         m_NSRemoveInputNormalization = (NSRemoveInputNormalization)GetProcAddress(m_hDLL, "removeInputNormalization");
  254.         m_NSRemoveOutputNormalization = (NSRemoveOutputNormalization)GetProcAddress(m_hDLL, "removeOutputNormalization");
  255.         m_NSGetInputNormMin = (NSGetInputNormMin)GetProcAddress(m_hDLL, "getInputNormMin");
  256.         m_NSSetInputNormMin = (NSSetInputNormMin)GetProcAddress(m_hDLL, "setInputNormMin");
  257.         m_NSGetInputNormMax = (NSGetInputNormMax)GetProcAddress(m_hDLL, "getInputNormMax");
  258.         m_NSSetInputNormMax = (NSSetInputNormMax)GetProcAddress(m_hDLL, "setInputNormMax");
  259.         m_NSGetOutputNormMin = (NSGetOutputNormMin)GetProcAddress(m_hDLL, "getOutputNormMin");
  260.         m_NSSetOutputNormMin = (NSSetOutputNormMin)GetProcAddress(m_hDLL, "setOutputNormMin");
  261.         m_NSGetOutputNormMax = (NSGetOutputNormMax)GetProcAddress(m_hDLL, "getOutputNormMax");
  262.         m_NSSetOutputNormMax = (NSSetOutputNormMax)GetProcAddress(m_hDLL, "setOutputNormMax");
  263.         m_NSGetNormalizeInputByChannel = (NSGetNormalizeInputByChannel)GetProcAddress(m_hDLL, "getNormalizeInputByChannel");
  264.         m_NSSetNormalizeInputByChannel = (NSSetNormalizeInputByChannel)GetProcAddress(m_hDLL, "setNormalizeInputByChannel");
  265.         m_NSGetNormalizeOutputByChannel = (NSGetNormalizeOutputByChannel)GetProcAddress(m_hDLL, "getNormalizeOutputByChannel");
  266.         m_NSSetNormalizeOutputByChannel = (NSSetNormalizeOutputByChannel)GetProcAddress(m_hDLL, "setNormalizeOutputByChannel");
  267.         m_NSGetCrossValidationCostData = (NSGetCrossValidationCostData)GetProcAddress(m_hDLL, "getCrossValidationCostData");
  268.         m_NSGetCostData = (NSGetCostData)GetProcAddress(m_hDLL, "getCostData");
  269.         m_NSGetNumberOfEpochsTrained = (NSGetNumberOfEpochsTrained)GetProcAddress(m_hDLL, "getNumberOfEpochsTrained");
  270.         m_NSGetEpochOfBestCost = (NSGetEpochOfBestCost)GetProcAddress(m_hDLL, "getEpochOfBestCost");
  271.         m_NSSeedRandom = (NSSeedRandom)GetProcAddress(m_hDLL, "seedRandom");
  272.  
  273.         //---------------------------------------------------------------------------
  274.         // Verify functions unique to learning DLLs loaded correctly.
  275.         // Note: Do not free DLL on failure so that caller can use IsLoaded()/IsInitialized().
  276.         //---------------------------------------------------------------------------
  277.         if (!m_NSTrain ||
  278.             !m_NSGetBestCost ||
  279.             !m_NSSetBestCost ||
  280.             !m_NSGetBestWeightsPathName ||
  281.             !m_NSSetBestWeightsPathName ||
  282.             !m_NSGetSaveBestWeightsEnabled ||
  283.             !m_NSSetSaveBestWeightsEnabled ||
  284.             !m_NSGetSaveBestWeightsForTraining ||
  285.             !m_NSSetSaveBestWeightsForTraining ||
  286.             !m_NSGetCrossValidationEnabled ||
  287.             !m_NSSetCrossValidationEnabled ||
  288.             !m_NSGetAutoComputeInputNormCoeff ||
  289.             !m_NSSetAutoComputeInputNormCoeff ||
  290.             !m_NSGetAutoComputeOutputNormCoeff ||
  291.             !m_NSSetAutoComputeOutputNormCoeff ||
  292.             !m_NSRemoveInputNormalization ||
  293.             !m_NSRemoveOutputNormalization ||
  294.             !m_NSGetInputNormMin ||
  295.             !m_NSSetInputNormMin ||
  296.             !m_NSGetInputNormMax ||
  297.             !m_NSSetInputNormMax ||
  298.             !m_NSGetOutputNormMin ||
  299.             !m_NSSetOutputNormMin ||
  300.             !m_NSGetOutputNormMax ||
  301.             !m_NSSetOutputNormMax ||
  302.             !m_NSGetNormalizeInputByChannel ||
  303.             !m_NSSetNormalizeInputByChannel ||
  304.             !m_NSGetNormalizeOutputByChannel ||
  305.             !m_NSSetNormalizeOutputByChannel ||
  306.             !m_NSGetCrossValidationCostData ||
  307.             !m_NSGetCostData ||
  308.             !m_NSGetNumberOfEpochsTrained ||
  309.             !m_NSGetEpochOfBestCost ||
  310.             !m_NSSeedRandom)
  311.         {
  312.         }
  313.         //---------------------------------------------------------------------------
  314.         // If all functions available, create an instance of the neural network.
  315.         //---------------------------------------------------------------------------
  316.         else
  317.         {
  318.             m_NSCreateNetwork(m_pNetworkInstance,LEARNING);
  319.         }
  320.     }
  321. }
  322.  
  323. //---------------------------------------------------------------------------
  324. // CLASS: NSLearningNetwork            FUNCTION: RemoveInputNormalization()
  325. //---------------------------------------------------------------------------
  326. // Remove the normalization of the input data.
  327. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  328. //---------------------------------------------------------------------------
  329. int NSLearningNetwork::RemoveInputNormalization()
  330. {
  331.     if (!IsInitialized())
  332.         return -1;
  333.     return m_NSRemoveInputNormalization(m_pNetworkInstance);
  334. }
  335.  
  336. //---------------------------------------------------------------------------
  337. // CLASS: NSLearningNetwork            FUNCTION: RemoveOutputNormalization()
  338. //---------------------------------------------------------------------------
  339. // Remove the normalization of the output  data.
  340. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  341. //---------------------------------------------------------------------------
  342. int NSLearningNetwork::RemoveOutputNormalization()
  343. {
  344.     if (!IsInitialized())
  345.         return -1;
  346.     return m_NSRemoveOutputNormalization(m_pNetworkInstance);
  347. }
  348.  
  349. //---------------------------------------------------------------------------
  350. // CLASS: NSLearningNetwork            FUNCTION: SeedRandom(unsigned int)
  351. //---------------------------------------------------------------------------
  352. // Seed the random number generator.
  353. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  354. //---------------------------------------------------------------------------
  355. int NSLearningNetwork::SeedRandom(unsigned int seed)
  356. {
  357.     if (!IsInitialized())
  358.         return -1;
  359.     return m_NSSeedRandom(m_pNetworkInstance, seed);
  360. }
  361.  
  362. //---------------------------------------------------------------------------
  363. // CLASS: NSLearningNetwork            FUNCTION: Train(int,int,float*,float*)
  364. //---------------------------------------------------------------------------
  365. // Train the network without cross-validation.
  366. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  367. //---------------------------------------------------------------------------
  368. int NSLearningNetwork::Train(int epochs, int exemplars, float* inputData, float* desiredData)
  369. {
  370.     if (!IsInitialized())
  371.         return -1;
  372.     return m_NSTrain(m_pNetworkInstance,epochs,exemplars,inputData,desiredData,0,0,0);
  373. }
  374.  
  375. //---------------------------------------------------------------------------
  376. // CLASS: NSLearningNetwork            FUNCTION: Train(int,int,float*,float*,int,float*,float*)
  377. //---------------------------------------------------------------------------
  378. // Train the network with cross-validation.
  379. // NOTE: SetCrossValidationEnabled(true) must be called for cross-validation to be activated.
  380. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  381. //---------------------------------------------------------------------------
  382. int NSLearningNetwork::Train(int epochs, int exemplars, float* inputData, float* desiredData, int cvExemplars, float* cvInputData, float* cvDesiredData)
  383. {
  384.     if (!IsInitialized())
  385.         return -1;
  386.     return m_NSTrain(m_pNetworkInstance,epochs,exemplars,inputData,desiredData,cvExemplars,cvInputData,cvDesiredData);
  387. }
  388.  
  389. //---------------------------------------------------------------------------
  390. // CLASS: NSLearningNetwork        FUNCTION: GetAutoComputeInputNormCoeff()
  391. //---------------------------------------------------------------------------
  392. // Return whether or not the input normalization coefficients will
  393. // automatically be calculated.
  394. //---------------------------------------------------------------------------
  395. bool NSLearningNetwork::GetAutoComputeInputNormCoeff()
  396. {
  397.     if (!IsInitialized())
  398.         return false;
  399.     bool autoComputeInputNormCoeff;
  400.     if (m_NSGetAutoComputeInputNormCoeff(m_pNetworkInstance,autoComputeInputNormCoeff))
  401.         return false;
  402.     return autoComputeInputNormCoeff;
  403. }
  404.  
  405. //---------------------------------------------------------------------------
  406. // CLASS: NSLearningNetwork        FUNCTION: GetAutoComputeOutputNormCoeff()
  407. //---------------------------------------------------------------------------
  408. // Return whether or not the output normalization coefficients will
  409. // automatically be calculated.
  410. //---------------------------------------------------------------------------
  411. bool NSLearningNetwork::GetAutoComputeOutputNormCoeff()
  412. {
  413.     if (!IsInitialized())
  414.         return false;
  415.     bool autoComputeOutputNormCoeff;
  416.     if (m_NSGetAutoComputeOutputNormCoeff(m_pNetworkInstance,autoComputeOutputNormCoeff))
  417.         return false;
  418.     return autoComputeOutputNormCoeff;
  419. }
  420.  
  421. //---------------------------------------------------------------------------
  422. // CLASS: NSLearningNetwork        FUNCTION: GetBestCost()
  423. //---------------------------------------------------------------------------
  424. // Return the best cost or -1 on failure.
  425. //---------------------------------------------------------------------------
  426. float NSLearningNetwork::GetBestCost()
  427. {
  428.     float bestCost;
  429.     if (!IsInitialized())
  430.         return -1;
  431.     if (m_NSGetBestCost(m_pNetworkInstance,bestCost))
  432.         return -1;
  433.     return bestCost;
  434. }
  435.  
  436. //---------------------------------------------------------------------------
  437. // CLASS: NSLearningNetwork        FUNCTION: GetBestWeightsPathName()
  438. //---------------------------------------------------------------------------
  439. // Return the best weights path name or an empty string on failure.
  440. //---------------------------------------------------------------------------
  441. CString NSLearningNetwork::GetBestWeightsPathName()
  442. {
  443.     char buffer[1024];
  444.     int bufferUsed;
  445.     if (!IsInitialized())
  446.         return "";
  447.     if (m_NSGetBestWeightsPathName(m_pNetworkInstance,buffer,sizeof(buffer),bufferUsed))
  448.         return "";
  449.     return buffer;
  450. }
  451.  
  452. //---------------------------------------------------------------------------
  453. // CLASS: NSLearningNetwork        FUNCTION: GetCostData(float *)
  454. //---------------------------------------------------------------------------
  455. // Fills the costData array with the training learning curve (an array of cost
  456. // data for the training dataset). You must allocate the memory for the cost
  457. // data before passing its pointer to this function. Use GetNumberOfEpochsTrained
  458. // to determine the necessary size of the array.
  459. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  460. //---------------------------------------------------------------------------
  461. int NSLearningNetwork::GetCostData(float *costData)
  462. {
  463.     if (!IsInitialized())
  464.         return -1;
  465.     return m_NSGetCostData(m_pNetworkInstance, costData);
  466. }
  467.  
  468. //---------------------------------------------------------------------------
  469. // CLASS: NSLearningNetwork        FUNCTION: GetCrossValidationCostData(float *)
  470. //---------------------------------------------------------------------------
  471. // Fills the CVCostData array with the cross validation learning curve (an
  472. // array of cost data for the cross validation dataset). You must allocate
  473. // the memory for the cost data before passing its pointer to this function.
  474. // Use GetNumberOfEpochsTrained to determine the necessary size of the array.
  475. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  476. //---------------------------------------------------------------------------
  477. int NSLearningNetwork::GetCrossValidationCostData(float *CVCostData)
  478. {
  479.     if (!IsInitialized())
  480.         return -1;
  481.     return m_NSGetCrossValidationCostData(m_pNetworkInstance, CVCostData);
  482. }
  483.  
  484. //---------------------------------------------------------------------------
  485. // CLASS: NSLearningNetwork        FUNCTION: GetCrossValidationEnabled()
  486. //---------------------------------------------------------------------------
  487. // Return whether or not cross validation will be used during training.
  488. //---------------------------------------------------------------------------
  489. bool NSLearningNetwork::GetCrossValidationEnabled()
  490. {
  491.     if (!IsInitialized())
  492.         return false;
  493.     bool crossValidationEnabled;
  494.     if (m_NSGetCrossValidationEnabled(m_pNetworkInstance,crossValidationEnabled))
  495.         return false;
  496.     return crossValidationEnabled;
  497. }
  498.  
  499. //---------------------------------------------------------------------------
  500. // CLASS: NSLearningNetwork        FUNCTION: GetEpochOfBestCost()
  501. //---------------------------------------------------------------------------
  502. // Return the epoch during which the best cost was obtained.
  503. //---------------------------------------------------------------------------
  504. int NSLearningNetwork::GetEpochOfBestCost()
  505. {
  506.     if (!IsInitialized())
  507.         return -1;
  508.     int epochOfBestCost;
  509.     m_NSGetEpochOfBestCost(m_pNetworkInstance, epochOfBestCost);
  510.     return epochOfBestCost;
  511. }
  512.  
  513. //---------------------------------------------------------------------------
  514. // CLASS: NSLearningNetwork        FUNCTION: GetInputNormMax()
  515. //---------------------------------------------------------------------------
  516. // Returns the upper bound for the input normalization.
  517. //---------------------------------------------------------------------------
  518. float NSLearningNetwork::GetInputNormMax()
  519. {
  520.     if (!IsInitialized())
  521.         return -1;
  522.     float inputNormMax;
  523.     m_NSGetInputNormMax(m_pNetworkInstance, inputNormMax);
  524.     return inputNormMax;
  525. }
  526.  
  527. //---------------------------------------------------------------------------
  528. // CLASS: NSLearningNetwork        FUNCTION: GetInputNormMin()
  529. //---------------------------------------------------------------------------
  530. // Returns the lower bound for the input normalization.
  531. //---------------------------------------------------------------------------
  532. float NSLearningNetwork::GetInputNormMin()
  533. {
  534.     if (!IsInitialized())
  535.         return -1;
  536.     float inputNormMin;
  537.     m_NSGetInputNormMin(m_pNetworkInstance, inputNormMin);
  538.     return inputNormMin;
  539. }
  540.  
  541. //---------------------------------------------------------------------------
  542. // CLASS: NSLearningNetwork        FUNCTION: GetNormalizeInputByChannel()
  543. //---------------------------------------------------------------------------
  544. // Return whether or not the input normalization coefficients will
  545. // calculated by channel (for each column of data individually) or across the
  546. // entire input dataset.
  547. //---------------------------------------------------------------------------
  548. bool NSLearningNetwork::GetNormalizeInputByChannel()
  549. {
  550.     if (!IsInitialized())
  551.         return false;
  552.     bool normalizeInputByChannel;
  553.     if (m_NSGetNormalizeInputByChannel(m_pNetworkInstance, normalizeInputByChannel))
  554.         return false;
  555.     return normalizeInputByChannel;
  556. }
  557.  
  558. //---------------------------------------------------------------------------
  559. // CLASS: NSLearningNetwork        FUNCTION: GetNormalizeOutputByChannel()
  560. //---------------------------------------------------------------------------
  561. // Return whether or not the output normalization coefficients will
  562. // calculated by channel (for each column of data individually) or across the
  563. // entire output dataset.
  564. //---------------------------------------------------------------------------
  565. bool NSLearningNetwork::GetNormalizeOutputByChannel()
  566. {
  567.     if (!IsInitialized())
  568.         return false;
  569.     bool normalizeOutputByChannel;
  570.     if (m_NSGetNormalizeOutputByChannel(m_pNetworkInstance, normalizeOutputByChannel))
  571.         return false;
  572.     return normalizeOutputByChannel;
  573. }
  574.  
  575. //---------------------------------------------------------------------------
  576. // CLASS: NSLearningNetwork        FUNCTION: GetNumberOfEpochsTrained()
  577. //---------------------------------------------------------------------------
  578. // Returns the number of epochs that the network has been trained for (the
  579. // network may not train for the complete number of epochs set due to the
  580. // existance of transmitters that may stop the network early).
  581. //---------------------------------------------------------------------------
  582. int NSLearningNetwork::GetNumberOfEpochsTrained()
  583. {
  584.     if (!IsInitialized())
  585.         return -1;
  586.     int numberOfEpochsTrained;
  587.     m_NSGetNumberOfEpochsTrained(m_pNetworkInstance, numberOfEpochsTrained);
  588.     return numberOfEpochsTrained;
  589. }
  590.  
  591. //---------------------------------------------------------------------------
  592. // CLASS: NSLearningNetwork        FUNCTION: GetOutputNormMax()
  593. //---------------------------------------------------------------------------
  594. // Returns the upper bound for the output normalization.
  595. //---------------------------------------------------------------------------
  596. float NSLearningNetwork::GetOutputNormMax()
  597. {
  598.     if (!IsInitialized())
  599.         return -1;
  600.     float outputNormMax;
  601.     m_NSGetOutputNormMax(m_pNetworkInstance, outputNormMax);
  602.     return outputNormMax;
  603. }
  604.  
  605. //---------------------------------------------------------------------------
  606. // CLASS: NSLearningNetwork        FUNCTION: GetOutputNormMin()
  607. //---------------------------------------------------------------------------
  608. // Returns the upper bound for the output normalization.
  609. //---------------------------------------------------------------------------
  610. float NSLearningNetwork::GetOutputNormMin()
  611. {
  612.     if (!IsInitialized())
  613.         return -1;
  614.     float outputNormMin;
  615.     m_NSGetOutputNormMin(m_pNetworkInstance, outputNormMin);
  616.     return outputNormMin;
  617. }
  618.  
  619. //---------------------------------------------------------------------------
  620. // CLASS: NSLearningNetwork        FUNCTION: GetSaveBestWeightsEnabled()
  621. //---------------------------------------------------------------------------
  622. // Return whether or not the best weights will be saved during training.
  623. //---------------------------------------------------------------------------
  624. bool NSLearningNetwork::GetSaveBestWeightsEnabled()
  625. {
  626.     bool saveBestWeightsEnabled;
  627.     if (!IsInitialized())
  628.         return false;
  629.     if (m_NSGetSaveBestWeightsEnabled(m_pNetworkInstance,saveBestWeightsEnabled))
  630.         return false;
  631.     return saveBestWeightsEnabled;
  632. }
  633.  
  634. //---------------------------------------------------------------------------
  635. // CLASS: NSLearningNetwork        FUNCTION: GetSaveBestWeightsForTraining()
  636. //---------------------------------------------------------------------------
  637. // Return whether the best weights will be saved for training or cross validation.
  638. //---------------------------------------------------------------------------
  639. bool NSLearningNetwork::GetSaveBestWeightsForTraining()
  640. {
  641.     bool saveBestWeightsForTraining;
  642.     if (!IsInitialized())
  643.         return false;
  644.     if (m_NSGetSaveBestWeightsForTraining(m_pNetworkInstance,saveBestWeightsForTraining))
  645.         return false;
  646.     return saveBestWeightsForTraining;
  647. }
  648.  
  649. //---------------------------------------------------------------------------
  650. // CLASS: NSLearningNetwork        FUNCTION: SetAutoComputeInputNormCoeff(bool)
  651. //---------------------------------------------------------------------------
  652. // Turn the auto-computation of input normalization coefficients on or off.
  653. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  654. //---------------------------------------------------------------------------
  655. int NSLearningNetwork::SetAutoComputeInputNormCoeff(bool autoComputeInputNormCoeff)
  656. {
  657.     if (!IsInitialized())
  658.         return -1;
  659.     return m_NSSetAutoComputeInputNormCoeff(m_pNetworkInstance, autoComputeInputNormCoeff);
  660. }
  661.  
  662. //---------------------------------------------------------------------------
  663. // CLASS: NSLearningNetwork        FUNCTION: SetAutoComputeOutputNormCoeff(bool)
  664. //---------------------------------------------------------------------------
  665. // Turn the auto-computation of output normalization coefficients on or off.
  666. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  667. //---------------------------------------------------------------------------
  668. int NSLearningNetwork::SetAutoComputeOutputNormCoeff(bool autoComputeOutputNormCoeff)
  669. {
  670.     if (!IsInitialized())
  671.         return -1;
  672.     return m_NSSetAutoComputeOutputNormCoeff(m_pNetworkInstance, autoComputeOutputNormCoeff);
  673. }
  674.  
  675. //---------------------------------------------------------------------------
  676. // CLASS: NSLearningNetwork        FUNCTION: SetBestCost(float)
  677. //---------------------------------------------------------------------------
  678. // Set the best cost.
  679. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  680. //---------------------------------------------------------------------------
  681. int NSLearningNetwork::SetBestCost(float bestCost)
  682. {
  683.     if (!IsInitialized())
  684.         return -1;
  685.     return m_NSSetBestCost(m_pNetworkInstance,bestCost);
  686. }
  687.  
  688. //---------------------------------------------------------------------------
  689. // CLASS: NSLearningNetwork        FUNCTION: SetBestWeightsPathName(LPCSTR)
  690. //---------------------------------------------------------------------------
  691. // Set the path name to save the best weights to.
  692. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  693. //---------------------------------------------------------------------------
  694. int NSLearningNetwork::SetBestWeightsPathName(LPCSTR pathName)
  695. {
  696.     if (!IsInitialized())
  697.         return -1;
  698.     return m_NSSetBestWeightsPathName(m_pNetworkInstance,pathName);
  699. }
  700.  
  701. //---------------------------------------------------------------------------
  702. // CLASS: NSLearningNetwork        FUNCTION: SetCrossValidationEnabled(bool)
  703. //---------------------------------------------------------------------------
  704. // Turn the cross-validation on or off.
  705. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  706. //---------------------------------------------------------------------------
  707. int NSLearningNetwork::SetCrossValidationEnabled(bool crossValidationEnabled)
  708. {
  709.     if (!IsInitialized())
  710.         return -1;
  711.     return m_NSSetCrossValidationEnabled(m_pNetworkInstance,crossValidationEnabled);
  712. }
  713.  
  714. //---------------------------------------------------------------------------
  715. // CLASS: NSLearningNetwork        FUNCTION: SetInputNormMax(float)
  716. //---------------------------------------------------------------------------
  717. // Sets the upper bound for the input normalization.
  718. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  719. //---------------------------------------------------------------------------
  720. int NSLearningNetwork::SetInputNormMax(float inputNormMax)
  721. {
  722.     if (!IsInitialized())
  723.         return -1;
  724.     return m_NSSetInputNormMax(m_pNetworkInstance, inputNormMax);
  725. }
  726.  
  727. //---------------------------------------------------------------------------
  728. // CLASS: NSLearningNetwork        FUNCTION: SetInputNormMin(float)
  729. //---------------------------------------------------------------------------
  730. // Sets the lower bound for the input normalization.
  731. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  732. //---------------------------------------------------------------------------
  733. int NSLearningNetwork::SetInputNormMin(float inputNormMin)
  734. {
  735.     if (!IsInitialized())
  736.         return -1;
  737.     return m_NSSetInputNormMin(m_pNetworkInstance, inputNormMin);
  738. }
  739.  
  740. //---------------------------------------------------------------------------
  741. // CLASS: NSLearningNetwork        FUNCTION: SetNormalizeInputByChannel(bool)
  742. //---------------------------------------------------------------------------
  743. // Turn by-channel normalization of the input data on or off.
  744. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  745. //---------------------------------------------------------------------------
  746. int NSLearningNetwork::SetNormalizeInputByChannel(bool normalizeInputByChannel)
  747. {
  748.     if (!IsInitialized())
  749.         return -1;
  750.     return m_NSSetNormalizeInputByChannel(m_pNetworkInstance, normalizeInputByChannel);
  751. }
  752.  
  753. //---------------------------------------------------------------------------
  754. // CLASS: NSLearningNetwork        FUNCTION: SetNormalizeOutputByChannel(bool)
  755. //---------------------------------------------------------------------------
  756. // Turn by-channel normalization of the output data on or off.
  757. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  758. //---------------------------------------------------------------------------
  759. int NSLearningNetwork::SetNormalizeOutputByChannel(bool normalizeOutputByChannel)
  760. {
  761.     if (!IsInitialized())
  762.         return -1;
  763.     return m_NSSetNormalizeOutputByChannel(m_pNetworkInstance, normalizeOutputByChannel);
  764. }
  765.  
  766. //---------------------------------------------------------------------------
  767. // CLASS: NSLearningNetwork        FUNCTION: SetOutputNormMax(float)
  768. //---------------------------------------------------------------------------
  769. // Sets the upper bound for the output normalization.
  770. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  771. //---------------------------------------------------------------------------
  772. int NSLearningNetwork::SetOutputNormMax(float outputNormMax)
  773. {
  774.     if (!IsInitialized())
  775.         return -1;
  776.     return m_NSSetOutputNormMax(m_pNetworkInstance, outputNormMax);
  777. }
  778.  
  779. //---------------------------------------------------------------------------
  780. // CLASS: NSLearningNetwork        FUNCTION: SetOutputNormMin(float)
  781. //---------------------------------------------------------------------------
  782. // Sets the lower bound for the output normalization.
  783. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  784. //---------------------------------------------------------------------------
  785. int NSLearningNetwork::SetOutputNormMin(float outputNormMin)
  786. {
  787.     if (!IsInitialized())
  788.         return -1;
  789.     return m_NSSetOutputNormMin(m_pNetworkInstance, outputNormMin);
  790. }
  791.  
  792. //---------------------------------------------------------------------------
  793. // CLASS: NSLearningNetwork        FUNCTION: SetSaveBestWeightsEnabled(bool)
  794. //---------------------------------------------------------------------------
  795. // Turn the saving of the best weights on or off.
  796. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  797. //---------------------------------------------------------------------------
  798. int NSLearningNetwork::SetSaveBestWeightsEnabled(bool saveBestWeightsEnabled)
  799. {
  800.     if (!IsInitialized())
  801.         return -1;
  802.     return m_NSSetSaveBestWeightsEnabled(m_pNetworkInstance,saveBestWeightsEnabled);
  803. }
  804.  
  805. //---------------------------------------------------------------------------
  806. // CLASS: NSLearningNetwork        FUNCTION: SetSaveBestWeightsForTraining(bool)
  807. //---------------------------------------------------------------------------
  808. // Turn the saving of the best weights for training on or off.
  809. // Returns 0 on success, -1 if not initialized, or an error code from the DLL.
  810. //---------------------------------------------------------------------------
  811. int NSLearningNetwork::SetSaveBestWeightsForTraining(bool saveBestWeightsForTraining)
  812. {
  813.     if (!IsInitialized())
  814.         return -1;
  815.     return m_NSSetSaveBestWeightsForTraining(m_pNetworkInstance,saveBestWeightsForTraining);
  816. }
  817.  
  818. //===========================================================================
  819. //===========================================================================
  820. // CLASS: NSRecallNetwork
  821. //===========================================================================
  822. //===========================================================================
  823.  
  824. //---------------------------------------------------------------------------
  825. // CLASS: NSRecallNetwork        FUNCTION: <constructor> (LPCSTR)
  826. //---------------------------------------------------------------------------
  827. NSRecallNetwork::NSRecallNetwork(LPCSTR dllPathName):
  828.     NSNetwork(dllPathName)
  829. {
  830.     //---------------------------------------------------------------------------
  831.     // If DLL loaded successfully, create an instance of the neural network.
  832.     //---------------------------------------------------------------------------
  833.     if (IsLoaded())
  834.     {
  835.         m_NSCreateNetwork(m_pNetworkInstance,RECALL);
  836.     }
  837. }